//
//  ShellCoinViewModel.m
//  ShellWallert
//
//  Created by lvzhao on 2020/5/10.
//  Copyright © 2020 VV. All rights reserved.
//

#import "ShellCoinViewModel.h"


@interface ShellCoinViewModel()

@property (nonatomic,assign) NSInteger pageIndex;

@end

@implementation ShellCoinViewModel


- (instancetype)init
{
    self = [super init];
    if (self) {
       
    }
    return self;
}


//获取shell余额
- (void)getShellCoin{
        
    @weakify(self);
    [LZNetworkingManager lz_request:@"get" url:kReadWalletInfo params:@{@"coinCode":@"SHELL"} success:^(id  _Nullable responseObject) {
        @strongify(self);
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            self.wallertModel = [ShellWallertModel mj_objectWithKeyValues:responseObject[kResultData]];
            self.wallertModel.currencyType = ShellCurrencyTypeShell;
        }
        [self.reloadSubject sendNext:@(5)];

    } failure:^(NSError * _Nullable error) {
        @strongify(self);
        [self.reloadSubject sendNext:@(5)];

    } isFailTip:NO];
}

/**
 Convert shell
 @param  isFirst 是否是第一次
 */
- (void)getCoinList:(BOOL)isFirst{
    
    if (isFirst) {
        self.pageIndex = 1;
        [self.dataArray removeAllObjects];
    }

    NSDictionary *params = @{@"pageNum":@(self.pageIndex)};
    @weakify(self);
    [LZNetworkingManager lz_request:@"get" url:kCrowdList params:params success:^(id  _Nullable responseObject) {
        @strongify(self);
        NSArray *dataArray = responseObject[kResultData];
        if(isFirst){
            [self.dataArray removeAllObjects];
        }
        for (int i = 0; i < dataArray.count; i++) {
            ShellCoinModel *coinModel = [ShellCoinModel mj_objectWithKeyValues:dataArray[i]];
            [self.dataArray addObject:coinModel];
        }
       //分页查询没有更多数据, 或者是当前页面  ==  后台返回的总页数
       NSInteger totalCount = [responseObject[@"total"] integerValue];
       if(self.dataArray.count == totalCount && self.dataArray.count > 0){
           //没有更多数据
           [self.reloadSubject sendNext:@(0)];
           return;
       }
       //看是否有数据
       if(self.dataArray.count == 0){
           [self.reloadSubject sendNext:@(0)];
       } else {
           self.pageIndex++;
           [self.reloadSubject sendNext:@(1)];
       }
       
   } failure:^(NSError * _Nullable error) {
       
       [self.reloadSubject sendNext:@(0)];
       
   } notwork:^(NSError * _Nullable error) {
       [self.reloadSubject sendNext:@(0)];
       
   } isLoading:NO isFailTip:YES];

    
}
@end
